home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Doors_System / Frontend / Main.c < prev    next >
C/C++ Source or Header  |  1996-10-31  |  10KB  |  413 lines

  1. /*
  2.  
  3.   Functions of the frontend door are thus:
  4.  
  5.   - Ask for system and node password and display PrivateSystem and PrivateNode
  6.     screens is appropriate.
  7.   - To Ask User for Username
  8.   - Check the user name is in the userdatabase
  9.     - if not then call new user door
  10.   - ask user for password.
  11.   - Check password is correct.
  12.     - if not then call hack door
  13.   - fill in N_ND->User with the correct data.
  14.   - check if user is allowed to logon
  15.     - Access Level >= N_ND->AccessRequired ?
  16.     - Allowed at this time ?
  17.   - reset user->TimeUsed to 0 if last called date is not the same as today..
  18.   - return one of the following strings.
  19.     - LOGGEDIN
  20.     - HACK        // if HACK then run "HACK" system door..
  21.     - FAILED
  22.   - check to see if there is an access level the same as the user, if not
  23.     flag the sysop and downgrade the user's access level to the next lowest one.
  24.  
  25.   todo:
  26.  
  27.   put max password and username attempts in config file.
  28.   put min/max username/password lengths in config file.
  29.  
  30.  
  31.   NamesNotAllowedFile, with an option for allowretry.. eg.
  32.  
  33.   Name_1=ALL
  34.   Retry_1=YES
  35.   Name_2=SUNNY
  36.   Retry_2=NO    ;-)
  37.  
  38.  
  39. */
  40.  
  41. #include <exec/types.h>
  42. #include <exec/memory.h>
  43. #include <clib/exec_protos.h>
  44. #include <clib/alib_protos.h>
  45.  
  46. #include <dos/dos.h>
  47. #include <clib/dos_protos.h>
  48.  
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #include <time.h>
  54.  
  55.  
  56. #ifdef __SASC
  57. int CXBRK(void) { return(0); }
  58. int _CXBRK(void) { return(0); }
  59. void chkabort(void) {}
  60. #endif
  61.  
  62. #include <HBBS/Defines.h>
  63. #include <HBBS/types.h>
  64. #include <HBBS/structures.h>
  65. #include <HBBS/hbbscommon_protos.h>
  66. #include <HBBS/hbbscommon_pragmas.h>
  67. #include <HBBS/Hbbsnode_protos.h>
  68. #include <HBBS/Hbbsnode_pragmas.h>
  69. #include <HBBS/release.h>
  70. char *versionstr="$VER: FrontEnd "RELEASE_STR;
  71.  
  72. struct Library *HBBSCommonBase=NULL;
  73. struct Library *HBBSNodeBase=NULL;
  74.  
  75. struct BBSGlobalData *BBSGlobal=NULL;
  76. struct NodeData *N_ND=NULL;
  77. int N_NodeNum=-1;
  78.  
  79. char Username[80];
  80. char Password[80];
  81. short MinUsernameLen=3;
  82. char tmpstr[256];
  83. char *HangupCommand=NULL;
  84. struct UserData User;
  85. char *HandleToShortMsg=NULL;
  86. char *InvalidHandleMsg=NULL;
  87. char *NameCorrectMsg=NULL;
  88.  
  89. static VOID cleanup(ULONG num)
  90. {
  91.   if (HBBSNodeBase)
  92.   {
  93.     HBBS_CleanUpDoor();
  94.     CloseLibrary (HBBSNodeBase);
  95.   }
  96.  
  97.   if (HBBSCommonBase)
  98.   {
  99.     HBBS_CleanUpCommon();
  100.     CloseLibrary (HBBSCommonBase);
  101.   }
  102.  
  103.   if (num) printf("Door Error = %d\n",num);
  104.  
  105.   exit(0);
  106. }
  107.  
  108. static VOID init(char *name)
  109. {
  110.   if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  111.   {
  112.     cleanup(1);
  113.   }
  114.  
  115.   if (!(HBBS_InitCommon()))
  116.   {
  117.     cleanup(2);
  118.   }
  119.  
  120.   if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  121.   {
  122.     cleanup(3);
  123.   }
  124.  
  125.   if (!(HBBS_InitDoor(N_NodeNum,name)))
  126.   {
  127.     cleanup(4);
  128.   }
  129.   SetProgramName(name);
  130. }
  131.  
  132.  
  133. /********************************* ACTUAL DOOR CODE **************************/
  134.  
  135. int FE_GetUsername( void )
  136. {
  137.   do
  138.   {
  139.     DOOR_WriteText(N_ND->NodeSettings.UserNamePrompt);
  140.     if (DOOR_GetLine(GL_NOOLM|GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',0,0,NULL)==IN_LOSSCARRIER) // Lost Carrier ?
  141.     {
  142.       return(-1);
  143.     }
  144.     else
  145.     {
  146.       // Copy input to our own variable.
  147.  
  148.       strNcpy(Username,N_ND->CurrentLine,79); // 79 == Max Username Len..
  149.       RemoveSpaces(Username);
  150.     }
  151.  
  152.     if (stricmp(Username,HangupCommand)==0)
  153.     {
  154.       DOOR_HangUp();
  155.       return(-1);
  156.     }
  157.  
  158.     if (strlen(Username)<MinUsernameLen)
  159.     {
  160.       DOOR_WriteText(HandleToShortMsg);
  161.       DOOR_WriteText("\r\n");
  162.     }
  163.     else // ok, username is long enough
  164.     {
  165.       return(1);
  166.     }
  167.  
  168.   } while (TRUE);
  169. }
  170.  
  171. int FE_GetPassword(char *Prompt,char *ActualPassword)
  172. {
  173.   short passwordattempts;
  174.  
  175.   for (passwordattempts=1;passwordattempts<=BBSGlobal->MaxPasswordAttempts;passwordattempts++)
  176.   {
  177.     DOOR_WriteText(Prompt);
  178.     if (DOOR_GetLine(GL_NOOLM|GL_EDIT|GL_DISPLAY,'*',0,30,NULL)==IN_LOSSCARRIER) // *C* make the 30 a configurable item
  179.     {
  180.       return(-1);
  181.     }
  182.     else
  183.     {
  184.       strNcpy(Password,N_ND->CurrentLine,79);
  185.  
  186.       // compare users actual password to the string (case insensitive) in Password
  187.  
  188.       if (stricmp(Password,ActualPassword)==0)
  189.       {
  190.         return(1);
  191.       }
  192.       else
  193.       {
  194.         sprintf(tmpstr,"Wrong Password, you have %d tr%s left\r\n",BBSGlobal->MaxPasswordAttempts-passwordattempts,BBSGlobal->MaxPasswordAttempts-passwordattempts!=1 ? "ies" : "y");
  195.         DOOR_WriteText(tmpstr);
  196.       }
  197.     }
  198.   }
  199.   return(0);
  200. }
  201.  
  202. void DisplayBaudScreen( void )
  203. {
  204.   char filename[20];
  205.  
  206.   sprintf(filename,"Speed_%s",N_ND->ConnectBaud);
  207.   DOOR_DisplaySpecialScreen(filename);
  208. }
  209.  
  210. #define HCK_NONE 0
  211. #define HCK_TIME 1
  212. #define HCK_LOCK 2
  213. #define HCK_PSWD 3
  214.  
  215. void DoorMain( void )
  216. {
  217.   BOOL LostCarrier=FALSE;
  218.   BOOL LoggedIn=FALSE;
  219.   int Hack=HCK_NONE;
  220.   BOOL SkipPW;
  221.   short LoginAttempts=0;
  222.   BOOL OK,ReTry=TRUE;
  223.   struct CfgFileData *CfgFile;
  224.  
  225.  
  226.   HandleToShortMsg=DupStr("That name/handle is too short. Please try again, or type BYE to hangup.");
  227.   InvalidHandleMsg=DupStr("That name/handle contains invalid characters, try again, or type BYE to hangup");
  228.   NameCorrectMsg=DupStr("Did you enter your name correctly ? ");
  229.   HangupCommand=DupStr("BYE");
  230.  
  231.   if (CfgFile=HBBS_LoadConfig("ProgDir:FrontEnd.CFG",LCFG_NONE))
  232.   {
  233.     HBBS_GetSetting(CfgFile,(void *)&HandleToShortMsg,VTYPE_STRING,"HandleToShortMsg",OPT_SINGLE);
  234.     HBBS_GetSetting(CfgFile,(void *)&NameCorrectMsg,VTYPE_STRING,"NameCorrectMsg",OPT_SINGLE);
  235.     HBBS_GetSetting(CfgFile,(void *)&InvalidHandleMsg,VTYPE_STRING,"InvalidHandleMsg",OPT_SINGLE);
  236.     HBBS_GetSetting(CfgFile,(void *)&HangupCommand,VTYPE_STRING,"HangupCommand",OPT_SINGLE);
  237.  
  238.     HBBS_FlushConfig(CfgFile);
  239.   }
  240.  
  241.   if (N_ND->NodeSettings.UseSysPW)
  242.   {
  243.     DOOR_DisplaySpecialScreen(SSCREEN_PRIVATESYSTEM);
  244.     OK=(FE_GetPassword(N_ND->NodeSettings.SysPWPrompt,N_ND->NodeSettings.SysPW)==1);
  245.   }
  246.  
  247.   if (OK && N_ND->NodeSettings.UseNodePW)
  248.   {
  249.     DOOR_DisplaySpecialScreen(SSCREEN_PRIVATENODE);
  250.     OK=(FE_GetPassword(N_ND->NodeSettings.NodePWPrompt,N_ND->NodeSettings.NodePW)==1);
  251.   }
  252.  
  253.   if (OK)
  254.   {
  255.     DisplayBaudScreen();
  256.   }
  257.  
  258.   if (OK && N_ND->OnlineStatus==OS_ONLINE)
  259.   {
  260.     DOOR_DisplaySpecialScreen("BBSTitle");
  261.  
  262.     do
  263.     {
  264.       switch(FE_GetUsername())
  265.       {
  266.         case -1:
  267.           LostCarrier=TRUE;
  268.           break;
  269.         case 1:
  270.           LoginAttempts++;
  271.           // ok, we got a sensible username lets see if it exists
  272.  
  273.           SkipPW=FALSE;
  274.           do
  275.           {
  276.  
  277.  
  278.             if (!HBBS_ValidUserHandle(Username,&User))
  279.             {
  280.               // user does not exist
  281.  
  282. //              if (ReTry)
  283.  
  284.               ReTry=FALSE;
  285.               if (DOOR_ContinuePrompt(NameCorrectMsg,DCP_ADDYN|DEFAULT_NO))
  286.               {
  287.                 if (!HBBS_HandleNameOK(Username))
  288.                 {
  289.                   DOOR_WriteText(InvalidHandleMsg); // display comments about ;'s and ,'s being in the file..
  290.                   DOOR_WriteText("\r\n");
  291.                 }
  292.                 else
  293.                 {
  294.                   DOOR_WriteText("Calling New User Door\r\n");
  295.                   DOOR_SystemDoor("NEWUSER",Username);
  296.                   if (iposition("USERADDED",N_ND->DoorReturn)!=-1)
  297.                   {
  298.                     ReTry=TRUE;
  299.                     SkipPW=TRUE;
  300.                     LoginAttempts--;
  301.                   }
  302.                 }
  303.               }
  304.             }
  305.             else
  306.             {
  307.               ReTry=FALSE;
  308.               // ok, copy the users data to the node's user pointer..
  309.  
  310.               CopyMem(&User,&N_ND->User.NormalData,sizeof(struct UserData));
  311.               CopyMem(&User,&N_ND->User.CallData,sizeof(struct UserData));
  312.               N_ND->User.Valid=TRUE; // that's not to say they've logged in ok yet tho... :-)
  313.  
  314.               // lets see if they are allowed to login..
  315.               // *C* add timecheck here! (and use HCK_TIME for hack)
  316.               // cool idea, call HACK door with a parameter, e.g. HACK PASSWORD, HACK NOTIME, HACK LOCKEDOUT etc..
  317.  
  318.               if (N_ND->User.CallData.Status==USER_NEW ||
  319.                   N_ND->User.CallData.Status==USER_VALIDATED ||
  320.                   N_ND->User.CallData.Status==USER_INACTIVE)
  321.               {
  322.                 // user exists, now lets try getting a password.
  323.  
  324.                 if (N_ND->NodeSettings.AskUserPW && !SkipPW)
  325.                 {
  326.                   // but only if we're supposed to...
  327.                   switch(FE_GetPassword(N_ND->NodeSettings.UserPWPrompt,User.Password))
  328.                   {
  329.                     case -1:
  330.                       DOOR_WriteText("Lost Carrier!\r\n");
  331.                       LostCarrier=TRUE;
  332.                       break;
  333.                     case 0:
  334.                       DOOR_WriteText("Hack Attempt\r\n");
  335.                       Hack=HCK_PSWD;
  336.                       break;
  337.                     case 1:
  338.                       LoggedIn=TRUE;
  339.                       break;
  340.                   }
  341.  
  342.  
  343.  
  344.                 } else LoggedIn=TRUE; //skip password and log user in..
  345.               }
  346.               else
  347.               {
  348.                 // Logins are denied for this user!
  349.                 Hack=HCK_LOCK;
  350.               }
  351.             }
  352.           } while (ReTry);
  353.           break;
  354.       }
  355.       if (N_ND->OnlineStatus==OS_OFFLINE) LostCarrier=TRUE;
  356.     } while (!LostCarrier && !LoggedIn && !Hack && LoginAttempts<BBSGlobal->MaxUsernameAttempts);
  357.  
  358.     FreeStr(HandleToShortMsg);
  359.     FreeStr(InvalidHandleMsg);
  360.     FreeStr(HangupCommand);
  361.     FreeStr(NameCorrectMsg);
  362.  
  363.     if (LoggedIn)
  364.     {
  365.       HBBS_SetAccess();
  366.       DOOR_Return("LOGGEDIN");
  367.       return;
  368.     }
  369.     if (Hack)
  370.     {
  371.       // Call Hack Attempt Door (so that the user has a chance to mail the sysop)
  372.       tmpstr[0]=0;
  373.       switch (Hack)
  374.       {
  375.         case HCK_TIME:
  376.           strcpy(tmpstr,"TIME");
  377.           break;
  378.         case HCK_PSWD:
  379.           strcpy(tmpstr,"PSWD");
  380.           break;
  381.         case HCK_LOCK:
  382.           strcpy(tmpstr,"LOCK");
  383.           break;
  384.       }
  385.       strcat(tmpstr," ");
  386.       strcat(tmpstr,Username);
  387.       DOOR_SystemDoor("HACK",tmpstr);
  388.       DOOR_Return("HACK");
  389.       return;
  390.     }
  391.   }
  392.   DOOR_Return("FAILED");
  393. }
  394.  
  395. int main(int argc,char *argv[])
  396. {
  397.   if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  398.   {
  399.     printf("Invalid/No Paramaters for door!\n");
  400.     exit (20);
  401.   }
  402.   init("FrontEnd!");
  403.  
  404.   if (BBSGlobal=HBBS_GimmeBBS())
  405.   {
  406.     if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
  407.     {
  408.       DoorMain();
  409.     }
  410.   }
  411.   cleanup(0);
  412. }
  413.